package com.wes.dsptest;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.wes.dsptest.camera.CameraActivity;
import com.wes.dsptest.data.TinySharedPreferences;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TestFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TestFragment extends Fragment implements View.OnClickListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    private static final int RB_DEFAULT = 0;
    private static final int RB_CAMERA = 1;
    private static final int RB_DISK = 2;

    // TODO: Rename and change types of parameters
    private int rb_item = R.id.rb_default;
    private Context context;
    private String mParam1;
    private View rootView;
    private TextView tvText;
    private Button btn_Camera, btn_Disk, btn_default;
    private ScrollView scView;
    private ConstraintLayout ctrLayout;
    private RadioGroup rgItem;
    private ArrayList<LinearLayout> rbLinearLayout;

    public TestFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @return A new instance of fragment TestFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TestFragment newInstance(String param1) {
        TestFragment fragment = new TestFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if(rootView == null)
            rootView = inflater.inflate(R.layout.fragment_test, container, false);
        initData();
        // Inflate the layout for this fragment
        initView();

        return rootView;
    }

    private void initData() {
        TinySharedPreferences tsp = TinySharedPreferences.instance(context);
        rb_item = tsp.getIntValue("hardware_item", rb_item);
    }

    private void setRadioGroupIndex(int posiion) {
        TinySharedPreferences tsp = TinySharedPreferences.instance(context);
        tsp.setIntValue("hardware_item", posiion);
    }

    private void initDefaultView(LinearLayout defaultLayout) {
        ctrLayout.addView(defaultLayout,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        Button btnTest = new Button(context);
        btnTest.setText("default");
        btnTest.setId(RB_DEFAULT+10);
        defaultLayout.addView(btnTest,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        defaultLayout.setVisibility(View.GONE);
        btnTest.setOnClickListener(this);
    }

    private void initCameraView(LinearLayout cameraLayout) {
        ctrLayout.addView(cameraLayout,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        btn_Camera = new Button(context);
        btn_Camera.setText("开启相机");
        btn_Camera.setId(RB_CAMERA+100);
        cameraLayout.addView(btn_Camera,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        cameraLayout.setVisibility(View.GONE);
        btn_Camera.setOnClickListener(this);
    }

    private void initDiskView(LinearLayout diskLayout) {
        ctrLayout.addView(diskLayout,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        btn_Disk = new Button(context);
        btn_Disk.setText("Disk");
        btn_Disk.setId(RB_DISK+1000);
        diskLayout.addView(btn_Disk,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        diskLayout.setVisibility(View.GONE);
        btn_Disk.setOnClickListener(this);
    }

    private void setOneLayoutVisib(ArrayList<LinearLayout> rbLayout, int id, int visib){
        for(int i = 0; i < rbLayout.size(); i++){
            if(i == id){
                rbLayout.get(i).setVisibility(visib);
            }
            else
            {
                rbLayout.get(i).setVisibility(View.GONE);
            }
        }
    }


    private void initView() {
        scView = rootView.findViewById(R.id.sc_view);
        ctrLayout = rootView.findViewById(R.id.ctr_layout);
        rbLinearLayout = new ArrayList<>();
        rgItem = rootView.findViewById(R.id.rg_item);
        rgItem.check(rb_item);
        // item
        rgItem.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                setCheckChangeLayout(checkedId);
                setRadioGroupIndex(checkedId);
            }
        });

        rbLinearLayout.add(new LinearLayout(context));
        initDefaultView(rbLinearLayout.get(RB_DEFAULT));
        rbLinearLayout.add(new LinearLayout(context));
        initCameraView(rbLinearLayout.get(RB_CAMERA));
        rbLinearLayout.add(new LinearLayout(context));
        initDiskView(rbLinearLayout.get(RB_DISK));
        setCheckChangeLayout(rb_item);
    }

    private void setCheckChangeLayout(int checkedId) {
        switch (checkedId){
            case R.id.rb_default:
                setOneLayoutVisib(rbLinearLayout, RB_DEFAULT, View.VISIBLE);
                break;
            case R.id.rb_camera:
                setOneLayoutVisib(rbLinearLayout, RB_CAMERA, View.VISIBLE);
                break;
            case R.id.rb_disk:
                setOneLayoutVisib(rbLinearLayout, RB_DISK, View.VISIBLE);

                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case RB_CAMERA+100:
                Intent intent = new Intent(context, CameraActivity.class);
                startActivityForResult(intent, 12);
                break;
            case RB_DISK+1000:
                break;
        }
    }
}
